﻿using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using QServices.Services;
using QServices;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
using HandyControl.Controls;
using HandyControl.Data;

namespace QControlCenter.ViewModel
{
    public partial class HomePageViewModel : QBaseVM
    {
        [ObservableProperty]
        private string? region;

        [ObservableProperty]
        private int seasonalPattern;
        [ObservableProperty]
        private decimal temperatureOutdoor;
        [ObservableProperty]
        private decimal temperatureIndoor;
        [ObservableProperty]
        private decimal humidity;
        [ObservableProperty]
        private decimal co2;
        [ObservableProperty]
        private decimal o3;
        [ObservableProperty]
        private string shaJunModel;

        private Timer minute_Timer;
        public ObservableCollection<UseModel> UseModels { get; set; }


        public HomePageViewModel()
        {
            UseModels = new ObservableCollection<UseModel>(new List<UseModel>
            {
                new UseModel{Name="手动模式",Value=0},
                new UseModel{Name="春秋模式",Value=1},
                new UseModel{Name="夏季模式",Value=2},
                new UseModel{Name="冬季模式",Value=3}
            });
            ShaJunModel = "/images/o3shajun_stop.svg";
			//ShaJunModel = "/images/o3shajun_runing.svg";

			minute_Timer= new Timer(MinuteTimerHandle, null, 1000 * 60, 1000 * 60);


		}

		private void MinuteTimerHandle(object args)
        {
            if (string.IsNullOrWhiteSpace(Region)) return;
            try
            {
                var res=PlcManager.GetQMitsubishiPlc(Region).GetHomePageData();
                TemperatureOutdoor = res.temperature_out;
                TemperatureIndoor = res.temperature_in;
                Co2 = res.co2;
                O3 = res.o3;
                Humidity = res.humidy;

            }
            catch(Exception ex)
            {
                AppRuntime.Logger_Error(ex);
				Growl.ErrorGlobal($"读取plc数据失败");
			}
        }


		[RelayCommand]
        public void SelectModel()
        {
            AppRuntime.GetService().GetService<DeviceManagerService>().UpdateSeasonalPattern(Region,SeasonalPattern);
			Growl.SuccessGlobal("保存成功!");
            AppRuntime.Logger_Info("设置季节模式");
		}

       

        [RelayCommand]
        public void ChangeShaJunModel()
        {
            if (ShaJunModel.IndexOf("stop") > 0)
            {
                ShaJunModel = "/images/o3shajun_runing.svg";
            }
            else
            {
                ShaJunModel = "/images/o3shajun_stop.svg";
            }
        }


        public void InitData()
        {
			if (string.IsNullOrWhiteSpace(Region)) return;
			var info = AppRuntime.GetService().GetService<DeviceManagerService>().GetDosingEquipment(Region);
			if (info == null) return;
			this.UpdateFrom(info);
		}



    }




    public class UseModel
    {
        public string Name { get; set; }
        public int Value { get; set; }
    }
}
